package com.fly;

import java.io.IOException;
import java.net.InetAddress;

import javax.servlet.ServletContext;

import org.apache.commons.lang3.SystemUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.web.context.WebApplicationContext;

@EnableCaching
@SpringBootApplication
public class J2CacheApplication implements CommandLineRunner
{
    @Autowired
    ServletContext servletContext;
    
    @Value("${server.port}")
    Integer port;
    
    @Autowired
    WebApplicationContext applicationContext;
    
    public static void main(String[] args)
    {
        SpringApplication.run(J2CacheApplication.class, args);
    }
    
    @Override
    @CacheEvict(value = "users", allEntries = true)
    public void run(String... args)
        throws IOException
    {
        if (SystemUtils.IS_OS_WINDOWS && port > 0)
        {
            String ip = InetAddress.getLocalHost().getHostAddress();
            String url = "http://" + ip + ":" + port + servletContext.getContextPath();
            Runtime.getRuntime().exec("cmd /c start /min " + url);
            Runtime.getRuntime().exec("cmd /c start /min " + url + "/doc.html");
        }
    }
}
